package main.java.tmi.returnProduct.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import main.java.tmi.common.service.UserVO;
import main.java.tmi.returnProduct.service.ReturnProductService;
import main.java.tmi.returnProduct.service.ReturnProductVO;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

/**
 * 사용자 로그인 처리하는 서비스 구현 클래스
 *
 * @author min™
 * @since 2011. 07. 24
 *
 */
@Service
public class ReturnProductServiceImpl implements ReturnProductService {

	
	@Resource(name = "returnProductDAO")
	protected ReturnProductDAO returnProductDAO;	

	public List<ReturnProductVO> getReturnProductDetailView(Map<String,String> param) throws SQLException {
		
		return returnProductDAO.getReturnProductDetailView(param);
	}

	@Override
	public boolean addReturnProduct(Map<String, String> param, HttpServletRequest request) throws Exception {
		boolean resultData = true; 
		UserVO userInfo = (UserVO) RequestContextHolder.getRequestAttributes().getAttribute("userInfo", RequestAttributes.SCOPE_SESSION);
		String userIdTmp = "";
		if(userInfo != null) {
			userIdTmp = userInfo.getUserId();
		} else {
			userIdTmp = "SYSTEM";
		}
		
		int outNo = 0;
		try{
			param.put("registId", userIdTmp);
			String saveStatus = param.get("saveStatus").toString();
	    	System.out.println("=======================> saveStatus"+saveStatus);               
			//출고 상세 
						
			String goodsCodeTmp[] = request.getParameterValues("goodsCodeDetail");
			String mawbTmp[] = request.getParameterValues("mawbDetail");
			String hawbTmp[] = request.getParameterValues("hawbDetail");
			String batchNoTmp[] = request.getParameterValues("batchNoDetail");
			String returnQtTmp[] 	  = request.getParameterValues("returnQtyDetail");
			String realsedQtyTmp[] = request.getParameterValues("realsedQtyDetail");
			String returnDateTmp[] = request.getParameterValues("returnDateDetail");
			String endUserTmp[] = request.getParameterValues("endUserDetail");
			String causeTmp[] = request.getParameterValues("causeDetail");
			String remarkTmp[] = request.getParameterValues("remarkDetail");
 
			String outNoTmp[] = request.getParameterValues("outNoDetail");
			String outSeqTmp[] = request.getParameterValues("outSeqDetail");
			String outDetSeqTmp[] = request.getParameterValues("outDetSeqDetail");
			
			System.out.println("===========>goodsCodeTmp.length: "+ goodsCodeTmp.length);
			String returnNo = returnProductDAO.getDetailSeq(param);
			if(goodsCodeTmp.length > 0){
				Map detailParamMap = new HashMap();
				for (int i=0 ; i < goodsCodeTmp.length ; i++){
					
					if ( StringUtils.isNotEmpty(goodsCodeTmp[i]) ){
						detailParamMap.clear();	
						
						
						detailParamMap.put("returnNo", returnNo );
						detailParamMap.put("goodsCode", goodsCodeTmp[i] );
						detailParamMap.put("mawb", mawbTmp[i] );
						detailParamMap.put("hawb", hawbTmp[i] );
						detailParamMap.put("batchNo", batchNoTmp[i] );
						detailParamMap.put("returnQty", returnQtTmp[i] );
						
						detailParamMap.put("realsedQty", realsedQtyTmp[i] );
						detailParamMap.put("returnDate", returnDateTmp[i] );
						detailParamMap.put("endUser", endUserTmp[i] );
						detailParamMap.put("cause", causeTmp[i] );
						detailParamMap.put("remark", remarkTmp[i] );
						detailParamMap.put("sesseionId", userIdTmp);
						
						detailParamMap.put("outNo", outNoTmp[i]);
						detailParamMap.put("outSeq", outSeqTmp[i]);
						detailParamMap.put("outDetSeq", outDetSeqTmp[i]);
						
						returnProductDAO.addReturnProductDetail(detailParamMap);
					}
				}
			}

		}catch(Exception e){
			resultData = false;
		}
		return resultData;
	}

	@Override
	public boolean modifyReturnProductDetail(Map<String, String> param) throws Exception {
		boolean resultData = true; 
		try{
			returnProductDAO.modifyReturnProductDetail(param);
		}catch(Exception e){
			resultData = false;
		}
		return resultData;
	}

	@Override
	public boolean delReturnProductDetail(Map<String, String> param) throws Exception {
		boolean resultData = true; 
		try{
			returnProductDAO.delReturnProductDetail(param);
		}catch(Exception e){
			resultData = false;
		}
		return resultData;
	}
}
